/*
 * This file is part of org.kalmeo.apps.kaprice.
 * 
 * org.kalmeo.apps.kaprice is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * org.kalmeo.apps.kaprice is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with org.kalmeo.apps.kaprice.  If not, see <http://www.gnu.org/licenses/>.
 *  
 * Creation date : 12 mars 08
 * Copyright (c) Kalmeo 2007-2008. All rights reserved.
 */

package com.ilove.weather.frame;

import org.kalmeo.kuix.core.Kuix;
import org.kalmeo.kuix.core.KuixConstants;
import org.kalmeo.kuix.core.model.DataProvider;
import org.kalmeo.kuix.widget.ListItem;
import org.kalmeo.kuix.widget.Widget;
import org.kalmeo.util.frame.Frame;
import com.ilove.core.ILOVE;
import com.ilove.weather.*;

/**
 * @author omarino
 */
public class ProvinceFrame implements Frame {

	static int posSelected = 0;	// Province selected
	public String areaName = null;
	
	// static instance of MainFrame
	public static ProvinceFrame instance = new ProvinceFrame();

	private Pad pad = new Pad();
	private int level = 1;

	/**
	 * Show the screen
	 */
	public void showScreen() {
		if (ILOVE.screenProvince != null) {
			ILOVE.screenProvince.setCurrent();
		}
	}

	
	/* (non-Javadoc)
	 * @see org.kalmeo.util.frame.Frame#onMessage(java.lang.Object, java.lang.Object[])
	 */
	public boolean onMessage(Object identifier, Object[] arguments) {
		if ("showMainScreen".equals(identifier)) {
			showScreen();
			return false;
		} else if ("askQuit".equals(identifier)) {
			// show askQuit box wich call onMessage method with "quit" name and null arguments
			Kuix.alert(	Kuix.getMessage("ASK_QUIT"),
					KuixConstants.ALERT_QUESTION | KuixConstants.ALERT_YES | KuixConstants.ALERT_NO,
					"!exit", 
					null);
			return false;
		} else if ("next".equals(identifier)) {
			if (level==1) {
				level = 2;
				Widget focusedWidget = ILOVE.screenProvince.getFocusManager().getFocusedWidget();
				if (focusedWidget instanceof ListItem) {
					DataProvider dataprovider = ((ListItem) focusedWidget).getDataProvider();
					if (dataprovider != null) {
						Province province = (Province) dataprovider;
						if ("Không có thông tin...".equalsIgnoreCase(province.name)) return false;
//						try {
//							pad.waiting(this);
//						} catch (Exception e1) {
//						}
						try {
							ILOVE.screenProvince.setTitle(pad.getProvince3Day(posSelected, province.id));
							ILOVE.screenProvince.setDataProvider(pad);
//							Kuix.getFrameHandler().removeFrame(WaitingFrame.instance);
							showScreen();
						} catch (Exception e) {
							Kuix.alert("Không load được dữ liệu. Vui lòng thử lại sau.");
						}
					}
				}
			}
			return false;
		} else if ("back".equals(identifier)) {
			if (level == 1) {
				Kuix.getFrameHandler().removeFrame(instance);
			} else if (level == 2) {
				level = 1;
				pad.callBack();
				ILOVE.screenProvince.setDataProvider(pad);
				ILOVE.screenProvince.setTitle(areaName);
			}
			return false;
		} else if ("about".equals(identifier)) {
			Kuix.alert("Phần mềm cung cấp thông tin Dự Báo Thời Tiết. Phiên bản 1.0");
			return false;
		} else if ("member".equals(identifier)) {
			String UID = RMSManager.readUID();
			Kuix.alert("Tài khoản của bạn: " + (UID == null ? "Chưa đăng ký" : UID));
			return false;
		}
		return true;
	}
	/* (non-Javadoc)
	 * @see org.kalmeo.util.frame.Frame#onAdded()
	 */
	public void onAdded() {
		ILOVE.UID = RMSManager.readUID();
		level = 1;
		try {
//			pad.waiting(this);
			if (pad.getProvince(posSelected)) {
				if (ILOVE.screenProvince == null) 
					ILOVE.screenProvince = Kuix.loadScreen(Constant.SCREEN_PROVINCE, pad);
//				WeatherMidlet.screenProvince.setDataProvider(pad);
				ILOVE.screenProvince.setTitle(areaName);
//				Kuix.getFrameHandler().removeFrame(WaitingFrame.instance);
				showScreen();
			} else Kuix.getFrameHandler().removeFrame(instance);
		} catch (Exception e) {
			Kuix.alert("Không load được dữ liệu. Vui lòng thử lại sau. " + e.getMessage());
			Kuix.getFrameHandler().removeFrame(instance);
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.kalmeo.util.frame.Frame#onRemoved()
	 */
	public void onRemoved() {
		try {
			MainFrame.instance.showScreen();
		} catch (Exception e) {
		}
	}

}
